<?php

namespace App\Http\Controllers\Front;

use App\Interfaces\AddressService;
use App\Interfaces\CartService;
use App\Interfaces\OrderService;
use App\Models\Goods;
use App\Models\Order;
use App\Interfaces\CouponService;
use Illuminate\Http\Request;
use App\Http\Requests\Front\CreatOrderRequest;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Auth,Log;

class OrderController extends Controller
{

    public function index(AddressService $addressService,OrderService $orderService, CouponService $couponService, Request $request)
    {

        //1 获取选中的商品

        $from = $request->get('from');

        if ($from=='detail'){
            $cartGoods = Session::get('fastPay');

        }else{
            $cartGoods = Session::get('cartGoods');
        }

        //获取收货地址



        $defaultAddress = $addressService->getUserDefaultAddress(Auth::user()->id);

        if($defaultAddress){
            $info = $orderService->getBuyGoodsInfo($cartGoods,$defaultAddress->address_id,[],Auth::user()->id);
            // $info = $orderService->getBuyGoodsInfo($cartGoods,$defaultAddress->address_id,[],[],[],Auth::user()->id);
            $default_address_id = $defaultAddress->address_id;

        }else{
            $info = $orderService->getBuyGoodsInfo($cartGoods,0,[],Auth::user()->id);
            // $info = $orderService->getBuyGoodsInfo($cartGoods,0,[],[],[],Auth::user()->id);
            $default_address_id=null;
        }
// dd($info['orders']['eshopGoods']['discounts']->toArray());
        $address = $addressService->getUserAddress(Auth::user()->id);

        //优惠券
        $user_id = Auth::user()->id;
        $goods_id_arr = [];
        foreach ($cartGoods as $key => $value) {
            $goods_id = $value['goods_id'];
            $goods_id_arr[$key] = $value['goods_id'];
        }//end foreach()

// dd($info);
        return view('front.order.index',compact(['info','address','from','default_address_id', 'user_coupons']))
            ->withHeader('Cache-control', 'no-cache,no-store,must-revalidate')
            ->withHeader('Pragma','no-cache')
            ->withHeader('Expires','0');

    }


    public function update($address_id,AddressService $addressService,OrderService $orderService,Request $request){

        //1 获取选中的商品

        $from = $request->get('from');

        if ($from=='detail'){

            $cartGoods = Session::get('fastPay');
        }else{
            $cartGoods = Session::get('cartGoods');
        }

        //优惠券
        $coupons = [
            'e_coupon_id'=>$request->get('e_coupon_id'),
            'c_coupon_id'=>$request->get('c_coupon_id')
        ];

        //限时折扣
        /*$discounts = [
            'e_discount_id'=>$request->get('e_discount_id'),
            'c_discount_id'=>$request->get('c_discount_id')
        ];


        $current_discount_arr=[];

        if (@$request->get('discount_type')) {

            $current_discount_arr['discount_type'] = $request->get('discount_type');

            $current_discount_arr['goods_number'] = $request->get('goods_number');

            switch ($request->get('discount_type')) {

                case 'e':
                    $current_discount_arr['discount_id'] = $request->get('e_discount_id');
                    break;

                case 'c':
                    $current_discount_arr['discount_id'] = $request->get('c_discount_id');
                    break;
            }
        }*/

        //获取收货地址
        // $address = $addressService->getUserAddress(Auth::user()->id);

        $info = $orderService->getBuyGoodsInfo($cartGoods,$address_id,$coupons,Auth::user()->id);
        // $info = $orderService->getBuyGoodsInfo($cartGoods,$address_id,$coupons,$discounts,$current_discount_arr,Auth::user()->id);

        return response()->json(['info'=>$info]);


    }


    public function store(CreatOrderRequest $creatOrderRequest,OrderService $orderService,CartService $cartService){

        $toClearGoods = [];
        if ($creatOrderRequest->get('from')=='detail'){
            $cartGoods = Session::get('fastPay');
        }else{
            $cartGoods = Session::get('cartGoods');

            $toClearGoods = collect($cartGoods)->lists('goods_id')->toArray();
        }


        //优惠券
        $coupons = [
            'e_coupon_id'=>$creatOrderRequest->get('e_coupon_id'),
            'c_coupon_id'=>$creatOrderRequest->get('c_coupon_id')
        ];

        $order_ids = $orderService->createOrder(
            Auth::user()->id,
            $cartGoods,
            $creatOrderRequest->get('address_id'),
            $coupons
        );

        if ($order_ids){
            //生成批量支付单

            //清空购物车所选商品

            if ($creatOrderRequest->get('from')=='detail'){
                Session::put('fastPay',[]);
            }else{
                $cartService->removeGoods($toClearGoods);

            }

            return response()->json(['status'=>'success','order_pay_id'=>implode(',',$order_ids)]);
        }


    }



}
